use ${newdata}carownership_dataset_bergen, clear

/* Formatting data */
drop if $trmgroup == . | $trmgroup == 0
keep if year == 2014
replace wies = wies / 100000
replace wealth = wealth /1000000
gen maxeduc0 = (max_educ == 0)
gen maxeduc1 = (max_educ == 1)
gen maxeduc2 = (max_educ == 2)
gen maxeduc3 = (max_educ == 3)
gen maxeduc4 = (max_educ == 4)

foreach var in toll wies wealth {
	gen `var'_sum = `var'1 + `var'2
	replace `var'_sum = `var'1 if `var'_sum == .
	replace `var'_sum = `var'2 if `var'_sum == .
}
replace wies_sum = wies_sum / 100000
replace wealth_sum = wealth_sum / 1000000

/* === Storing results in ster files ======================================== */
local varlist bev nrbev ice cars
eststo clear
forvalues trm = 1(1)4 {
	eststo sum_${trmgroup}_`trm':  estpost summarize `varlist' if $trmgroup == `trm'
	foreach var in `varlist' {
		qui sum `var' if $trmgroup == `trm' 
		estadd scalar `var'_m = r(mean)
		estadd scalar `var'_sd = r(sd)
	}
	estimates save ${ster}tab2_col`trm'_outcome, replace
}
esttab 

eststo clear
local varlist toll toll_sum dist time_work PublicVSCarTime_fam_mean PublicDiffCarTime_fam_mean
forvalues trm = 1(1)4 {
	eststo sum_${trmgroup}_`trm':  estpost summarize `varlist' if $trmgroup == `trm'
	foreach var in `varlist' {
		qui sum `var' if $trmgroup == `trm' 
		estadd scalar `var'_m = r(mean)
		estadd scalar `var'_sd = r(sd)
	}
	estimates save ${ster}tab2_col`trm'_wd_var, replace
}

eststo clear
local varlist couple children antpers_i_regstat_famnr age kvinne secondhome employed retired wies wies_sum wealth wealth_sum
forvalues trm = 1(1)4 {
	eststo sum_${trmgroup}_`trm':  estpost summarize `varlist' if $trmgroup == `trm'
	foreach var in `varlist' {
		qui sum `var' if $trmgroup == `trm' 
		estadd scalar `var'_m = r(mean)
		estadd scalar `var'_sd = r(sd)
	}
	estimates save ${ster}tab2_col`trm'_demo, replace
}

eststo clear
local varlist maxeduc0 maxeduc1 maxeduc2 maxeduc3 maxeduc4
forvalues trm = 1(1)4 {
	eststo sum_${trmgroup}_`trm':  estpost summarize `varlist' if $trmgroup == `trm'
	foreach var in `varlist' {
		qui sum `var' if $trmgroup == `trm' 
		estadd scalar `var'_m = r(mean)
		estadd scalar `var'_sd = r(sd)
	}
	estimates save ${ster}tab2_col`trm'_educ, replace
}


/* === Reading from ster, writing to tex ==================================== */

eststo drop * 
eststo clear
forvalues trm = 1/4 {
	estimates use "${ster}tab2_col`trm'_outcome"
	eststo
}
esttab * ///
	using "${tables}tab2a.tex", ///
	cells("mean(fmt(%12.3fc)) sd(fmt(%12.3fc))") ///
	coeflabels( ///
		bev "Electric vehicle (0/1)" ///
		nrbev "Number of electric vehicles" ///
		ice "Number of ICE vehicles" ///
		cars "Total number of vehicles" ///
	) ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///label   nogap booktabs ///
	nomtitle  /// 
	prehead("") posthead("") collabels(none) ///
	replace

eststo clear	
forvalues trm = 1/4 {
	estimates use "${ster}tab2_col`trm'_wd_var"
	eststo
}
esttab * ///
	using "${tables}tab2b.tex", ///
	cells("mean(fmt(%12.2fc)) sd(fmt(%12.2fc))") ///
	order(toll toll_sum dist time_work PublicDiffCarTime_fam_mean PublicVSCarTime_fam_mean) ///
	coeflabels( ///
		toll "Toll rate (NOK/individual)" ///
		toll_sum "Toll rate (NOK/household)" ///
		dist "Driving distance (km)" ///
		time_work "Driving time (min)" ///
		PublicDiffCarTime_fam_mean "PT time minus driving time (min)" ///
		PublicVSCarTime_fam_mean "PT time divided by driving time" ///
	) ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///label   nogap booktabs ///
	nomtitle  /// 
	prehead("") posthead("") collabels(none) ///
	replace	
	
eststo clear	
forvalues trm = 1/4 {
	estimates use "${ster}tab2_col`trm'_demo"
	eststo
}
esttab * ///
	using "${tables}tab2c.tex", ///
	cells("mean(fmt(%12.2fc)) sd(fmt(%12.2fc))") ///
	coeflabels( ///
		couple "Couple (0/1)" ///
		children "Children living at home (0/1)" ///
		antpers_i_regstat_famnr "Persons in household" ///
		age  "Age" ///
		kvinne "Female (0/1)" ///
		secondhome "Owns second home" ///
		employed "Employed (0/1)" ///
		retired "Retired (0/1)" ///
		wies "Income (100,000 NOK/individual)" ///
		wies_sum "Income (100,000 NOK/household)" ///
		wealth "Wealth (mill NOK/individual)" ///
		wealth_sum "Wealth (mill NOK/household)" ///
	) ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///label   nogap booktabs ///
	nomtitle  /// 
	prehead("") posthead("") collabels(none) ///
	replace		
	
eststo clear	
forvalues trm = 1/4 {
	estimates use "${ster}tab2_col`trm'_educ"
	eststo
}
esttab * ///
	using "${tables}tab2d.tex", ///
	cells("mean(fmt(%12.2fc)) sd(fmt(%12.2fc))") ///
	coeflabels( ///
		maxeduc0 "Unknown (0/1)" ///
		maxeduc1 "Less than high school (0/1)" ///
		maxeduc2 "High school (0/1)" ///
		maxeduc3 "University $<$4 years (0/1)" ///
		maxeduc4 "University $>$4 years (0/1)" ///
	) ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///label   nogap booktabs ///
	nomtitle  /// 
	prehead("") posthead("") collabels(none) ///
	stats(N, ///
		label("Observations" ///
		) fmt(0) ) ///
	replace		


